package com.framework.service.yhbservice.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.terminalDao.YhbGoodsDao;
import com.framework.exception.RRException;
import com.framework.service.yhbservice.YhbGoodsService;
import com.framework.validator.Assert;
import com.framework.yhbEntity.ter.YhbGoodsInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional(rollbackFor = Exception.class)
@Service("yhbGoodsService")
public class YhbGoodsServiceImpl extends ServiceImpl<YhbGoodsDao, YhbGoodsInfo> implements YhbGoodsService {


    @Override
    public List<YhbGoodsInfo> queryGoodsList(Long deptId, Integer goodsType) {

        List<String> descs=new ArrayList<>();
        descs.add("sort");
        List<YhbGoodsInfo> list = baseMapper.selectList(new EntityWrapper<YhbGoodsInfo>()
                .eq("dept_id", deptId)
                .eq("is_show", 1)
                .eq(goodsType.intValue()!=0,"goods_type",goodsType)
                .orderDesc(descs)
        );
        return list;
    }

    @Override
    public Map<String, Object> queryGoodsInfo(Long deptId, Long goodsId) {
        Map<String,Object> map = new HashMap<>();

        YhbGoodsInfo goods = queryGoodsById(goodsId);
        Assert.isNull(goods,"未查询到商品信息");
        if(deptId.intValue() != goods.getDeptId().intValue()){
            throw new RRException("9000","商品渠道不一致");
        }

        map.put("goods",goods);
        return map;
    }
    @Override
    public YhbGoodsInfo queryGoodsById(Long goodsId) {
        if(goodsId==null){
            throw new RRException("9000","商品id不能为空");
        }
        return baseMapper.selectById(goodsId);
    }
}
